package leetcode;

import java.util.Arrays;

public class fuxieling {
    public void duplicateZeros(int[] nums) {
        if(nums==null||nums.length==0){
            return;
        }
        //第一步先找cur与dest
        int dest=-1;
        int cur=0;
        while(dest<nums.length-1){
            //1.先判断nums[cur]的值
            if(nums[cur]!=0){
                dest++;
                if(dest!=nums.length-1){
                    cur++;
                }
            }else{
                dest=dest+2;
                if(dest==nums.length){
                    nums[nums.length-1]=0;
                    dest=dest-2;
                    cur--;
                    break;
                }
                if(dest!=nums.length-1){
                    cur++;
                }

            }
        }
        //走到这里找到了cur与dest
        //从后往前复写
        while(cur!=-1){
            if(nums[cur]!=0){
                nums[dest]=nums[cur];
                cur--;
                dest--;
            }else{
                nums[dest]=0;
                nums[dest-1]=0;
                cur--;
                dest=dest-2;
            }
        }


    }

    public static void main(String[] args) {
//        int[] array={1,0,2,3,0,4,5,0};
        int[] array={0,0,0,0,1};
        fuxieling fuxieling=new fuxieling();
        fuxieling.duplicateZeros(array);
        System.out.println(Arrays.toString(array));

    }
}
